<?php 
	if(isset($message)){
		echo $message;
	} else {
?>

<h1>Employment</h1>
<?php echo form_open('user/employment');?>
<table>
	<?php if(isset($employment)){?>
	<tr>
		<td colspan="2">
			<table>
				<tr>
					<td>Company Name:</td>
					<td><input type="text" name="employer_name[]" value="<?php echo $employment->employer_name?>">
					<input type="hidden" name="id[]" value="<?php echo $employment->id?>">
					</td>
				</tr>
				<tr>
					<td>Date Started:</td>
					<td><input type="text" name="date_start[]" id="date_start" value="<?php echo $employment->date_start?>"></td>
				</tr>
				<tr>
					<td>Date Ended:</td>
					<td><input type="text" name="date_end[]" id="date_end" value="<?php echo $employment->date_end?>"></td>
				</tr>
				<tr>
					<td>Position:</td>
					<td><input type="text" name="position[]" value="<?php echo $employment->position?>"></td>
				</tr>
				<tr>
					<td>Job Description</td>
					<td>
						<textarea name="description[]"><?php echo $employment->description?></textarea>
						<?php echo form_hidden("method", 'update');?>
					</td>
				</tr>
			</table>
		</td>
		
	</tr>
	<?php } else { ?>
	<tr>
		<td colspan="2">
			<table>
				<tr>
					<td width="100">Company Name:</td>
					<td><input type="text" name="employer_name[]">
					<input type="hidden" name="id[]" value="0">
					</td>
				</tr>
				<tr>
					<td>Date Started:</td>
					<td><input type="text" name="date_start[]" id="date_start"></td>
				</tr>
				<tr>
					<td>Date Ended:</td>
					<td><input type="text" name="date_end[]" id="date_end"></td>
				</tr>
				<tr>
					<td>Position:</td>
					<td><input type="text" name="position[]"></td>
				</tr>
				<tr>
					<td valign="top" style="padding-top:5px;">Job Description:</td>
					<td>
						<textarea name="description[]" rows="4"></textarea>
						<?php echo form_hidden("method", 'add');?>
					</td>
				</tr>
			</table>
		</td>
		
	</tr>
	<?php } ?>
	<tr>
		<td colspan="2">
		<?php echo form_hidden("user_id", $user_id);?>
		<input type="submit" name="submit" class="button" value="Register"></td>
	</tr>
	
</table>


<?php echo form_close();?>
<?php } ?>
<script>
$(document).ready(function(){
	<?php if(isset($message)):?>
		setTimeout( function() {
			alert('<?php echo $method?>')
			<?php if(isset($method) && $method=='update'){?>
			updateData();
			<?php } else if(isset($method) && $method=='add'){?>
			appendData();
			<?php } else { ?>
			parent.$.fancybox.close();
			<?php }?>
		}, 2000 );
	<?php endif;?>

	<?php if(isset($method) && $method=='update'):?>
	
	function updateData(){
		
		employer_name = "<?php echo isset($employment)? $employment['employer_name'][0]: ''?>";
		date_start = "<?php echo isset($employment)? $employment['date_start'][0]: ''?>";
		date_end = "<?php echo isset($employment)? $employment['date_end'][0]: ''?>";
		position = "<?php echo isset($employment)? $employment['position'][0]: ''?>";

		description = <?php echo isset($employment)? json_encode($employment['description'][0]): ''?>;
		id = "<?php echo isset($employment)? $employment['id'][0]: ''?>";

		parent.$('tr#tr_employment_' + id).find('span[name^="employer_name"]').text(employer_name);
		parent.$('tr#tr_employment_' + id).find('span[name^="date_start"]').text(date_start);
		parent.$('tr#tr_employment_' + id).find('span[name^="date_end"]').text(date_end);
		parent.$('tr#tr_employment_' + id).find('span[name^="position"]').text(position);
		//parent.$('li#employment_li_' + id).find('textarea[name^="description"]').val(description);
		parent.highlight('tr#tr_employment_' + id);
		parent.$.fancybox.close();
		
	}
	<?php endif;?>
<?php if(isset($method) && $method=='add'):?>
	
	function appendData(){
		
		employer_name = "<?php echo isset($employment)? $employment['employer_name'][0]: ''?>";
		date_start = "<?php echo isset($employment)? $employment['date_start'][0]: ''?>";
		date_end = "<?php echo isset($employment)? $employment['date_end'][0]: ''?>";
		position = "<?php echo isset($employment)? $employment['position'][0]: ''?>";
		id = "<?php echo isset($employment_id)? $employment_id: ''?>";

		tr_count = parent.$('#employment_table tr').length;
		css = (tr_count % 2)?'odd':'even';
		
		tr = "<tr id='tr_employment_" + id +"' class='" + css +"'><td>" + employer_name + "</td><td>" + position + "</td><td>";
		tr += date_start + "</td><td>" + date_end + "</td><td>";
		tr += "<a href='javascript:;' onClick='editData(\"<?php echo isset($employment_id)? $employment_id: ''?>\",\"employment\")'>Edit</a> | <a href='javascript:;' onclick='deleteData(\"<?php echo isset($employment_id)? $employment_id: ''?>\",\"dependent\")' >Delete</a>";
		tr += "</td></tr>";

		
		parent.$('tr#tr_employment_' + id).find('span[name^="employer_name"]').text(employer_name);
		parent.$('tr#tr_employment_' + id).find('span[name^="date_start"]').text(date_start);
		parent.$('tr#tr_employment_' + id).find('span[name^="date_end"]').text(date_end);
		parent.$('tr#tr_employment_' + id).find('span[name^="position"]').text(position);
		parent.$('#employment_table').append(tr);
		parent.highlight('tr#tr_employment_' + id);
		parent.$.fancybox.close();
		
	}
	<?php endif;?>


	// datepicker...
	$("#date_start").datepicker();
	$("#date_end").datepicker();
	$("#date_start").datepicker( "option", "dateFormat", 'yy-mm-dd' );
	$("#date_end").datepicker( "option", "dateFormat", 'yy-mm-dd' );
	<?php if(isset($education->date_start)):?>
	$("#date_start").datepicker( "setDate" , "<?php echo $employment->date_start?>" );
	<?php endif; ?>
	<?php if(isset($education->date_end)):?>
	$("#date_end").datepicker( "setDate" , "<?php echo $employment->date_end?>" );
	<?php endif; ?>

	
})

</script>
